package haddle

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"zhihu/dao"
	"zhihu/restle"
)

func AddArticle(c *gin.Context) {
	var data restle.AddArticle
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": err.Error(),
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	article, err := dao.AddArticle(data.UserId, data.Title, data.Counter)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": article,
	})
}

func ListArticle(c *gin.Context) {

	id := c.Query("id")
	user_id := c.Query("user_id")
	atoi, _ := strconv.Atoi(id)
	atoi1, _ := strconv.Atoi(user_id)

	article, err := dao.ListArticle(atoi1, atoi)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": article,
	})
}

func LikeArticle(c *gin.Context) {
	var data restle.LikeArticle
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": err.Error(),
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	article, err := dao.LikeArticle(data.UserId, data.Id)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": article,
	})
}

func ArticleReview(c *gin.Context) {
	var data restle.ArticleReview
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": err.Error(),
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	review, err := dao.ArticleReview(data.UserId, data.ArticleId, data.Context)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": review,
	})
}

func ArticleReviews(c *gin.Context) {
	var data restle.ArticleReviewfu
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": err.Error(),
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	review, err := dao.ArticleReviewfu(data.UserId, data.ArticleId, data.FatherId, data.Context)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": review,
	})
}

func ArticleReviewslike(c *gin.Context) {
	var data restle.LikeArticle
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": err.Error(),
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	article, err := dao.ArticleReviewLike(data.UserId, data.Id)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": article,
	})
}

func ListArticleReviewslike(c *gin.Context) {

	user_id := c.Query("user_id")
	userId, _ := strconv.Atoi(user_id)
	article_id := c.Query("article_id")
	articleId, _ := strconv.Atoi(article_id)
	article, err := dao.ListArticleReviewLike(userId, articleId)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": article,
	})
}
